var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "stockId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl",
            name: "stockName",
        }
    }
};

var ztree;

var vm = new Vue({
    el:'#rrapp',
    data:{
        showList: true,
        title: null,
        erpStock:{
            parentName:null,
            parentId:0,
            type:null
        },
        e:{
            selfMerchantList:null,
			type: [
				{value: 1,label: '生产仓'},
				{value: 2,label: '售后仓'}
			],
        },
		erpStocktype: {
			types:{
				type:1,
			}
		},
    },
    methods: {
        getStock: function(stockId){
            //加载c仓库树
            $.get(baseURL + "material/erpstock/select", function(r){
                ztree = $.fn.zTree.init($("#stockTree"), setting, r.stockList);
                var node = ztree.getNodeByParam("stockId", vm.erpStock.parentId);
                if(node != null){
                    ztree.selectNode(node);
                    vm.erpStock.parentName = node.stockName;
                }
            })
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.menu = {parentName:"顶级仓库",parentId:0};
            vm.getStock();
        },
        update: function () {
            var stockId = getStockId();
            if(stockId == null){
                return ;
            }

            $.get(baseURL + "material/erpstock/info/"+stockId, function(r){
                vm.showList = false;
                vm.title = "修改";
                vm.erpStock = r.erpStock;

                vm.getStock();
            });
        },
        del: function () {
            var stockId = getStockId();
            if(stockId == null){
                return ;
            }
            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "material/erpstock/delete",
                    contentType: "application/json",
                    data: JSON.stringify(stockId),
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        query: function(){
            vm.reload();
        },
        saveOrUpdate: function () {
            if(vm.validator()){
                return ;
            }

            var url = vm.erpStock.stockId == null ? "material/erpstock/save" : "material/erpstock/update";
            $.ajax({
                type: "POST",
                url:  baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.erpStock),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        stockTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择仓库",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#stockLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级菜单
                    vm.erpStock.parentId = node[0].stockId;
                    vm.erpStock.parentName = node[0].stockName;

                    layer.close(index);
                }
            });
        },
        reload: function () {
            vm.showList = true;
            Stock.table.refresh();
        },
        validator: function () {
            if(isBlank(vm.erpStock.stockName)){
                alert("菜单名称不能为空");
                return true;
            }

            //菜单
            /*if(vm.erpStock.type === 1 && isBlank(vm.erpStock.url)){*/

				// if(isBlank(vm.erpStock.url)){
            //     alert("菜单URL不能为空");
            //     return true;
            // }
        },
    },
    mounted: function() {
        // 获取常量
        getConstant()
    }
});

var Stock = {
    id: "stockTable",
    table: null,
    layerIndex: -1
};


/**
 * 初始化表格的列
 */
Stock.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: '仓库ID', field: 'stockId', visible: false, align: 'center', valign: 'middle', width: '80px'},
        {title: '仓库名称', field: 'stockName', align: 'center', valign: 'middle', sortable: true, width: '180px'},
        {title: '父级仓库', field: 'parentName', align: 'center', valign: 'middle', sortable: true, width: '100px'},
        {title: '所属组织', field: 'institution', align: 'center', valign: 'middle', sortable: true, width: '100px',
            formatter: function (row) {
                var valueText = "";
                if(row.institution != ""){
                    $.each(vm.e.selfMerchantList, function (index, module) {
                        if (module.merchantId == row.institution) {
                            valueText = module.suppName;
                            return false;
                        }
                    });
                }
                return valueText;
            }
        },
        {title: '仓库状态', field: 'status', align: 'center', valign: 'middle', sortable: true, width: '160px',
            formatter: function (value, options, row) {
                if (value.status){
                    return '禁用';
                }
                return '启用';
            }
        }
        ]
    return columns;
};


function getStockId () {
    var selected = $('#stockTable').bootstrapTreeTable('getSelections');
    if (selected.length == 0) {
        alert("请选择一条记录");
        return null;
    } else {
        return selected[0].id;
    }
}


$(function () {
    var colunms = Stock.initColumn();
    var table = new TreeTable(Stock.id, baseURL + "material/erpstock/list", colunms);
    table.setExpandColumn(2);
    table.setIdField("stockId");
    table.setCodeField("stockId");
    table.setParentCodeField("parentId");
    table.setExpandAll(true);
    table.setHeight(700);
    table.init();
    Stock.table = table;

});

function getConstant (){
    $.ajax({
        type: "POST",
        url: baseURL + "supp/sellerinfo/selfList",
        data: null,
        success: function(r){
            console.log(r);
            vm.e.selfMerchantList = r;
        }
    });
}